﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using BlueCMS.Core.Data.Query.Generator;

namespace BlueCMS.Core.Data.Query
{
    public abstract class QueryBase
    {
        private string mSql;
        protected bool IsGenerated { get; private set; }
        static object locker = new object();

        public string Sql
        {
            get
            {
                if (String.IsNullOrEmpty(mSql))
                {
                    lock (locker)
                    {
                        if (!String.IsNullOrEmpty(mSql))
                        {
                            return mSql;
                        }

                        QueryGeneratorBase generator = DataAccess.Instance.GetQueryGenerator();
                        mSql = generator.Process(this);
                        IsGenerated = true;
                    }
                }
                return mSql;
            }
        }

        public override string ToString()
        {
            return Sql;
        }
    }
}
